package com.ebt.m.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.activity.LoginActivity;
import com.ebt.m.data.bean.UserInfo;
import com.ebt.m.utils.TalkingDataUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wanglu on 2016/12/20.
 */

public class PlusUnionView extends RelativeLayout {
    @BindView(R.id.inner_plus_union)
    ImageButton mPlusButton;
    @BindView(R.id.action_proposal)
    ImageButton mPlusProposal;
    @BindView(R.id.action_customer)
    ImageButton mPlusCustomer;
    @BindView(R.id.action_container)
    LinearLayout mActionContainer;

    private OnPlusClickLitener mPlusClickLitener;
    private RotateAnimation antiClockwiseAnimation;
    private RotateAnimation clockwiseAnimation;

    private TranslateAnimation translateAnimation;
    private TranslateAnimation antiTraslateAnimation;
    private GestureDetector gestureDetector;

    public PlusUnionView(Context context) {
        this(context, null);
    }

    public PlusUnionView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlusUnionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_plus_union, this);
        ButterKnife.bind(this);
        antiClockwiseAnimation = new RotateAnimation(135f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        antiClockwiseAnimation.setDuration(400);
        antiClockwiseAnimation.setRepeatCount(0);
        antiClockwiseAnimation.setFillAfter(true);
        clockwiseAnimation = new RotateAnimation(0f, 135f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        clockwiseAnimation.setDuration(400);
        clockwiseAnimation.setRepeatCount(0);
        clockwiseAnimation.setFillAfter(true);
        antiClockwiseAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (mPlusClickLitener != null)
                    mPlusClickLitener.onClick(mPlusButton);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        float translateDelta = getResources().getDimension(R.dimen.plus_traslate_dimen);
        translateAnimation = new TranslateAnimation(0f, 0f, translateDelta, 0f);
        translateAnimation.setDuration(300);
        translateAnimation.setRepeatCount(0);
        translateAnimation.setFillAfter(true);
        antiTraslateAnimation = new TranslateAnimation(0f, 0f, 0f, translateDelta);
        antiTraslateAnimation.setDuration(300);
        antiTraslateAnimation.setRepeatCount(0);
        antiTraslateAnimation.setFillAfter(true);
        gestureDetector = new GestureDetector(context, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return true;
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                mActionContainer.startAnimation(antiTraslateAnimation);
                mPlusButton.startAnimation(antiClockwiseAnimation);
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {

            }

            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }


    @OnClick({R.id.inner_plus_union, R.id.action_customer, R.id.action_proposal})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.inner_plus_union:
                mActionContainer.startAnimation(antiTraslateAnimation);
                mPlusButton.startAnimation(antiClockwiseAnimation);
                break;
            case R.id.action_customer:
                TalkingDataUtil.onEventWithLabel(TalkingDataUtil.TYPE_BOOTOM_PLUS,"bottom_plus_customer");
                if (mPlusClickLitener != null)
                    mPlusClickLitener.onClick(view);
                break;
            case R.id.action_proposal:
                TalkingDataUtil.onEventWithLabel(TalkingDataUtil.TYPE_BOOTOM_PLUS,"bottom_plus_proposal");
                if (mPlusClickLitener != null)
                    mPlusClickLitener.onClick(view);
                break;
        }

    }

    public void setOnPlusClickListener(OnPlusClickLitener listener) {
        this.mPlusClickLitener = listener;
    }

    public interface OnPlusClickLitener {
        void onClick(View view);
    }

    public void onResume() {
        mPlusButton.clearAnimation();
        mPlusButton.startAnimation(clockwiseAnimation);
        mActionContainer.startAnimation(translateAnimation);
    }
}
